#include <stdio.h>
#include <string.h>

int map[202][202];
int n;


int shu[30];


int out;

main()
{
	int a,b,i,j,k,len;

	char ss[30];

	shu[1]=1;
	for (i=2;i<=26;i++)
		shu[i]=shu[i-1]<<1;
	
	while (1)
	{
		scanf("%d", &n);
		if (n==0) break;
		memset(map,0,sizeof(map));
//		memset(lin,0,sizeof(lin));
//		memset(lian,0,sizeof(lian));
		while (1)
		{
			scanf("%d%d", &a, &b);
			if (a==0 && b==0) break;
			

			scanf("%s",ss);
			len=strlen(ss);
			for (i=0;i<len;i++)
				map[a][b]+=shu[ss[i]-'a'+1];
			

		}
		for (k=1;k<=n;k++)
			for (i=1;i<=n;i++)

				for (j=1;j<=n;j++)
					if (i!=j && j!=k && k!=i && ((map[i][k] & map[k][j])!=0) )
					{
						map[i][j]=map[i][j] | (map[i][k] & map[k][j]);
					}




		while (1)
		{
			scanf("%d%d", &a, &b);
			if (a==0 && b==0) break;
//			wfs(a,b);
			out=map[a][b];
			if (out==0) printf("-");
			else
			{
				for (i=1;i<=26;i++)
					if (out & shu[i])			
						printf("%c", i+'a'-1);					
			}
			printf("\n");
		}
		printf("\n");

	}
	
	return 0;
}

